package com.lottery.bootdo.common.controller.events;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lottery.bootdo.common.service.EventsInfoService;
import com.lottery.bootdo.common.utils.PageUtils;
import com.lottery.bootdo.common.utils.StringUtils;
import com.lottery.common.contants.ReQueryResult;
import com.lottery.common.entity.*;
import com.lottery.common.service.CustomInfoService;
import com.lottery.common.service.CustomPushRecordService;
import com.lottery.common.service.DecryptToObjectService;
import com.lottery.common.utils.RedisUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static com.lottery.common.contants.Constants.*;

@Controller
@RequestMapping("/custom")
@Slf4j
@Api(value = "/custom",tags="定制管理操作类")
public class CustomController {
	private ReQueryResult result = new ReQueryResult();


	@Autowired
	private DecryptToObjectService decryptToObjectService;
	@Autowired
	private CustomInfoService customInfoService;
	@Autowired
	private CustomPushRecordService customPushRecordService;
	@Autowired
	private EventsInfoService eventsInfoService;


	@RequiresPermissions("custom:list")
	@RequestMapping(value = "/custom", method = RequestMethod.GET)
	String index(Model model) {
		List<SysConfig> list= RedisUtils.getConfigFromRedisByType("00",SysConfig.class);
		model.addAttribute("list",list);
		return "common/recommend/custom/index";
	}


	@RequiresPermissions("custom:edit")
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	String edit(@PathVariable() Long id, Long memberId,Integer eventsType,Model model) {
//		List<SysConfig> list= RedisUtils.getConfigFromRedisByType("00",SysConfig.class);
		model.addAttribute("memberId",memberId);
		model.addAttribute("customId",id);
		model.addAttribute("customType",eventsType);

		return "common/recommend/custom/edit";
	}

	@RequestMapping(value = "", method = RequestMethod.GET)
	@RequiresPermissions("custom:list")
	@ResponseBody
	public PageUtils customList(String json){
		log.info("__________ customList");
		result = new ReQueryResult();
		NutMap nutMap=decryptToObjectService.jsonToObject(json,NutMap.class);
		log.info("customList nutmap is {}", JSON.toJSONString(nutMap));

		QueryWrapper<CustomInfo> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("status",TYPE_ZERO);
		if (StringUtils.isNotBlank(nutMap.getString("phone")))
			queryWrapper.like("phone",nutMap.get("phone"));
		if (StringUtils.isNotBlank(nutMap.getString("startTime"))){
			queryWrapper.apply("  create_time > '"+nutMap.getString("startTime")+"'");
		}
		if (StringUtils.isNotBlank(nutMap.getString("endTime"))){
			queryWrapper.apply("  create_time < '"+nutMap.getString("endTime")+"'");
		}

		queryWrapper.orderByDesc("id");

		Page<CustomInfo> page = new Page<>(nutMap.getInt("pageIndex"),nutMap.getInt("pageLength"));
		IPage<CustomInfo> records=customInfoService.page(page,queryWrapper);
		try{
			for(CustomInfo info:records.getRecords()){
				info.setType(RedisUtils.getConfigFromRedis(info.getCustomType(),SysConfig.class).getRemark());
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		log.info(JSON.toJSONString(records));
		PageUtils pageUtils = new PageUtils(records.getRecords(),new BigDecimal(records.getTotal()).intValue());
		return pageUtils;
	}

	@RequestMapping(value = "", method = RequestMethod.POST)
	@RequiresPermissions("custom:edit")
	@ResponseBody
	ReQueryResult custom(@RequestParam("ids[]") Long[] ids, Long memberId, Long customId, Integer customType) {
		log.info("__________ update");
		result = new ReQueryResult();
		List<CustomPushRecord> list=new ArrayList<>();
		for(Long s:ids){
			list.add(new CustomPushRecord(memberId,s,customType));
		}
		customPushRecordService.saveBatch(list);
		CustomInfo customInfo=new CustomInfo();
		customInfo.setId(customId);
		customInfo.setIsPush(TYPE_ZERO);
		customInfoService.updateById(customInfo);
		result.success(SUCCESS_DESC);
		return result;
	}


	@RequestMapping(value = "", method = RequestMethod.DELETE)
	@RequiresPermissions(value={"custom:del"},logical= Logical.OR)
	@ResponseBody
	ReQueryResult delete(CustomInfo info) {
		log.info("__________ delete");
		result = new ReQueryResult();
		info.setStatus(TYPE_ONE);
		customInfoService.updateById(info);
		result.success(SUCCESS_DESC);
		return result;
	}


	@RequestMapping(value = "/batchDel", method = RequestMethod.DELETE)
	@RequiresPermissions(value={"custom:del"},logical= Logical.OR)
	@ResponseBody
	public ReQueryResult batchDelete(@RequestParam("ids[]") Long[] ids){
		log.info("__________batchDel");
		result = new ReQueryResult();
		List<CustomInfo> list=new ArrayList<>();
		CustomInfo info;
		for (Long id:ids){
			info=new CustomInfo();
			info.setId(id);
			info.setStatus(TYPE_ONE);
			list.add(info);
		}
		customInfoService.updateBatchById(list);
		result.success(SUCCESS_DESC);
		return result;
	}


//	@RequestMapping(value = "recommend", method = RequestMethod.GET)
//	@RequiresPermissions(value={"custom:edit"},logical= Logical.OR)
//	@ResponseBody
//	public PageUtils customRecommend(String json){
//		log.info("__________ customRecommend");
//		result = new ReQueryResult();
//		NutMap nutMap=decryptToObjectService.jsonToObject(json,NutMap.class);
//		log.info("nutmap is {}", JSON.toJSONString(nutMap));
//
////		QueryWrapper<EventsInfo> queryWrapper=new QueryWrapper<>();
////		queryWrapper.eq("type",1).eq("events_type",1);
////		if (StringUtils.isNotBlank(nutMap.getString("name")))
////			queryWrapper.eq("name",nutMap.get("name"));
//		Page<EventsInfo> page = new Page<>(nutMap.getInt("pageIndex"),nutMap.getInt("pageLength"));
//
//		IPage<EventsInfo> records=eventsInfoService.getEventsInfoList(page,nutMap);
//		log.info(JSON.toJSONString(records));
//		PageUtils pageUtils = new PageUtils(records.getRecords(),new BigDecimal(records.getTotal()).intValue());
//		return pageUtils;
//	}

}
